#include "SpotL.h"
#include <cmath>

SpotL::SpotL( float l[3],float t[3],float angle, float exponent, float a[4], float d[4], float s[4], bool on, int lightX ):Light(a,d,s,on,lightX)
{
	float sum=0;
	for(int i=0; i<3; ++i){
		location[i]=l[i];
		target[i]=t[i]-location[i];
		sum+=target[i]*target[i];
	}
	location[3]=1;
	sum = sqrt(sum);
	for(int i=0; i<3; ++i)
		target[i]/=sum;

	this->angle=angle;
	this->exponent[0]=exponent;

	glLightfv(getLightX(), GL_AMBIENT, getAmbient());
	glLightfv(getLightX(), GL_DIFFUSE, getDiffuse());
	glLightfv(getLightX(), GL_SPECULAR, getSpecular());
}

void SpotL::act()
{
	glLightf(getLightX(), GL_SPOT_CUTOFF, angle);
	glLightfv(getLightX(), GL_SPOT_DIRECTION, target); 
	glLightfv(getLightX(), GL_POSITION, location);
	glLightfv(getLightX(), GL_SPOT_EXPONENT, exponent);
}
